package com.sl.ms.carriage.handler;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sl.ms.base.api.common.AreaFeign;
import com.sl.ms.base.domain.base.AreaDto;
import com.sl.ms.carriage.domain.constant.CarriageConstant;
import com.sl.ms.carriage.domain.dto.WaybillDTO;
import com.sl.ms.carriage.domain.enums.EconomicRegionEnum;
import com.sl.ms.carriage.entity.CarriageEntity;
import com.sl.ms.carriage.service.CarriageService;
import com.sl.transport.common.util.ObjectUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedHashMap;

/**
 * 经济区互寄
 */
@Order(300) //定义顺序
@Component
public class EconomicZoneChainHandler extends AbstractCarriageChainHandler {
    @Resource
    private CarriageService carriageService;
    @Resource
    private AreaFeign areaFeign;
    @Override
    public CarriageEntity doHandler(WaybillDTO waybillDTO) {
        Long receiverProvinceId = this.areaFeign.get(waybillDTO.getReceiverCityId()).getParentId();
        Long senderProvinceId = this.areaFeign.get(waybillDTO.getSenderCityId()).getParentId();
      CarriageEntity carriageEntity= this.carriageService.findEconomicCarriage(receiverProvinceId,senderProvinceId);
      if (carriageEntity != null)
      {
          return carriageEntity;
      }
        //return carriageEntity;
      return doNextHandler(waybillDTO);
    }

//    @Resource
//    private CarriageService carriageService;
//    @Resource
//    private AreaFeign areaFeign;
//
//    @Override
//    public CarriageEntity doHandler(WaybillDTO waybillDTO) {
//        CarriageEntity carriageEntity = null;
//
//        // 获取收寄件地址省份id
//        Long receiverProvinceId = this.areaFeign.get(waybillDTO.getReceiverCityId()).getParentId();
//        Long senderProvinceId = this.areaFeign.get(waybillDTO.getSenderCityId()).getParentId();
//
//        //获取经济区城市配置枚举
//        LinkedHashMap<String, EconomicRegionEnum> EconomicRegionMap = EnumUtil.getEnumMap(EconomicRegionEnum.class);
//        EconomicRegionEnum economicRegionEnum = null;
//        for (EconomicRegionEnum regionEnum : EconomicRegionMap.values()) {
//            //该经济区是否全部包含收发件省id
//            boolean result = ArrayUtil.containsAll(regionEnum.getValue(), receiverProvinceId, senderProvinceId);
//            if (result) {
//                economicRegionEnum = regionEnum;
//                break;
//            }
//        }
//
//        if (ObjectUtil.isNotEmpty(economicRegionEnum)) {
//            //根据类型编码查询
//            LambdaQueryWrapper<CarriageEntity> queryWrapper = Wrappers.lambdaQuery(CarriageEntity.class)
//                    .eq(CarriageEntity::getTemplateType, CarriageConstant.ECONOMIC_ZONE)
//                    .eq(CarriageEntity::getTransportType, CarriageConstant.REGULAR_FAST)
//                    .like(CarriageEntity::getAssociatedCity, economicRegionEnum.getCode());
//            carriageEntity = this.carriageService.getOne(queryWrapper);
//        }
//
//        return doNextHandler(waybillDTO, carriageEntity);
//    }
}
